package com.newworld.dmsapp2.fragment.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.fragment.bean.MenuItem;

/**
 * @author sunjingyu 主页面home的grid
 * 
 */
public class HomeMenuItemAdapter extends BaseAdapter {

	private List<MenuItem> menus;
	private LayoutInflater inflater;
	private int margin;

	public HomeMenuItemAdapter(Context context, List<MenuItem> menus, int margin) {
		inflater = LayoutInflater.from(context);
		this.menus = menus;
		this.margin = margin;
	}

	@Override
	public int getCount() {
		return menus.size();
	}

	@Override
	public Object getItem(int position) {
		return menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int height = parent.getHeight() / 3 - margin;
		AbsListView.LayoutParams param = new AbsListView.LayoutParams(
				LayoutParams.MATCH_PARENT, height);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.fragment_main_home_menuitem, parent, false);
			holder = new ViewHolder();
			holder.home_grid_item_image = (ImageView) convertView
					.findViewById(R.id.home_grid_item_image);
			holder.home_grid_item_title = (TextView) convertView
					.findViewById(R.id.home_grid_item_title);
			// holder.home_grid_item_content = (TextView)
			// convertView.findViewById(R.id.home_grid_item_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		MenuItem item = menus.get(position);
		holder.home_grid_item_image.setImageResource(item.menuIconRes);
		holder.home_grid_item_title.setText(item.menuTitle);
		/*
		 * if (item.menuMsg.length() == 0) {
		 * holder.home_grid_item_content.setVisibility(View.GONE); } else {
		 * holder.home_grid_item_content.setVisibility(View.VISIBLE);
		 * holder.home_grid_item_content.setText(item.menuMsg); }
		 */

		convertView.setLayoutParams(param);
		return convertView;
	}

	private class ViewHolder {
		ImageView home_grid_item_image;
		TextView home_grid_item_title;
		// TextView home_grid_item_content;
	}
}